package a4jsf;

import java.util.ArrayList;
import java.util.Collection;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class Form {

	   private String state;
	   private String country;

	   private UIInput statePrompt = new UIInput();
	   
	   private static final String US = "United States";
	   private static final String CANADA = "Canada";
	   private static final String[] COUNTRY_NAMES = { US, CANADA };
	   private static ArrayList<SelectItem> countryItems = null;
	  
	   // PROPERTY: countryNames
	   public Collection getCountryNames() {
	      if (countryItems == null) {
	         countryItems = new ArrayList<SelectItem>();
	         for (int i = 0; i < COUNTRY_NAMES.length; i++) {
	            countryItems.add(new SelectItem(COUNTRY_NAMES[i]));
	         }
	      }
	      return countryItems;
	   }
	   // PROPERTY: country
	   public void setCountry(String newValue) { country = newValue; }
	   public String getCountry() { return country; }

	   // PROPERTY: state
	   public void setState(String newValue) { state = newValue; }
	   public String getState() { return state; }


	   public void countryChanged(ValueChangeEvent event) {

		   FacesContext facesContext = FacesContext.getCurrentInstance();

		   // settin the choosen county.value to State.value using
		   // statePrompt(binding)
//		   this.statePrompt.setValueExpression("value", facesContext
//				   .getApplication().getExpressionFactory().createValueExpression(
//						   facesContext.getELContext(),
//						   (String) event.getNewValue(), String.class));
		   statePrompt.setValue(event.getNewValue());
		   
		   // this.state = (String) event.getNewValue();
		   if (US.equals((String) event.getNewValue()))
			   // context.getViewRoot().setLocale(Locale.US);
			   System.out.println("US");
		   else
			   // context.getViewRoot().setLocale(Locale.CANADA);
			   System.out.println("Canada");
	   }
	   
   // PROPERTY: statePrompt
	public UIInput getStatePrompt() {
		return statePrompt;
	}
	public void setStatePrompt(UIInput statePrompt) {
		this.statePrompt = statePrompt;
	}

}
